#include "../audio/audio.h"
#include "../thread/thread.h"
#include <unistd.h>
#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>

int init_suite(void){
    prepare_threads();
    init_sound();
    return 0;
}

int close_suite(void){
    close_threads();
    close_sound();
    return 0;
}

void audio_create_destroy(){
    sound_t* snd = create_sound_t(NULL, 1, 0, 1<<18);
    CU_ASSERT(snd == NULL);
    snd = create_sound_t("BLAH", 1, 0, 1<<18);
    CU_ASSERT(snd == NULL);
    snd = create_sound_t("data/bogus", 1, 0, 1<<18);
    CU_ASSERT(snd == NULL);
    snd = create_sound_t("data/fancy-pants.ogg", 1, 0, 1<<18);
    CU_ASSERT(snd != NULL);
    free_sound_t(&snd);
    CU_ASSERT(snd == NULL);
    snd = create_sound_t("data/fancy-pants.ogg", 1, 0, 1<<16);
    CU_ASSERT(snd == NULL);
}

void play_to_completion(){
    printf("You should not hear a loop:\n");
    sound_t *snd = create_sound_t("data/fancy-pants.ogg", 0, 0, 1<<18);
    CU_ASSERT(snd != NULL);
    play_sound(snd);
    sleep(3);
    free_sound_t(&snd);
    CU_ASSERT(snd == NULL);
}
void loop_play(){
    printf("You should hear a loop:\n");
    sound_t *snd = create_sound_t("data/fancy-pants.ogg", 1, 0, 1<<18);
    CU_ASSERT(snd != NULL);
    play_sound(snd);
    sleep(5);
    free_sound_t(&snd);
    CU_ASSERT(snd == NULL);
}
void streaming_loop_play(){
    printf("You should hear a streaming loop:\n");
    sound_t *snd = create_sound_t("data/fancy-pants.ogg", 1, 1, 1<<14);
    CU_ASSERT(snd != NULL);
    play_sound(snd);
    sleep(5);
    free_sound_t(&snd);
    CU_ASSERT(snd == NULL);
}

int main(int argc, char *argv[]){
    CU_pSuite pSuite = NULL;
    /* initialize the CUnit test registry */
    if (CUE_SUCCESS != CU_initialize_registry()){
        return CU_get_error();
    }
    pSuite = CU_add_suite("Audio Suite", init_suite, close_suite);
    if (NULL == pSuite) {
        CU_cleanup_registry();
        return CU_get_error();
    }
    if ((NULL == CU_add_test(pSuite, "test of audio create & destroy", audio_create_destroy)) ||
        (NULL == CU_add_test(pSuite, "test of play_to_completion()", play_to_completion)) ||
        //(NULL == CU_add_test(pSuite, "test of loop_play()", loop_play)) ||
        (NULL == CU_add_test(pSuite, "test of streaming_loop_play()", streaming_loop_play))
        ){
        CU_cleanup_registry();
        return CU_get_error();
    }
    
    /* Run all tests using the CUnit Basic interface */
    CU_basic_set_mode(CU_BRM_VERBOSE);
    CU_basic_run_tests();
    CU_cleanup_registry();
    return CU_get_error();
}
